package cn.wolfcode.wolf2w.business.listener;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.service.IStrategyService;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//ApplicationListener：程序监听器
//ApplicationReadyEvent:监听程序准备完成的时机点
@Component
public class ApplicationInitListener implements ApplicationListener<ApplicationReadyEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private RedisService redisService;

    //当应用程序准备完成的时候，会触发这个方法
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        List<Strategy> list = strategyService.list();
        for (Strategy strategy : list) {
            String key = RedisKeys.STRATEGY_STATIS_HASH.join(strategy.getId().toString());
            if (redisService.hasKey(key)){
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("viewnum", Integer.valueOf(strategy.getViewnum().toString()));
            map.put("replynum", Integer.valueOf(strategy.getReplynum().toString()));
            map.put("sharenum", Integer.valueOf(strategy.getSharenum().toString()));
            map.put("favornum", Integer.valueOf(strategy.getFavornum().toString()));
            map.put("thumbsupnum", Integer.valueOf(strategy.getThumbsupnum().toString()));
            map.put("id", strategy.getId());
            redisService.setCacheMap(key,map);
        }

    }


}
